package com.handpay.zztong.hp.bean;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.main.model.ratelist.RateListBean;

import se.krka.kahlua.vm.LuaTable;


public class Rates implements Serializable {

    private static final long serialVersionUID = -1122576365916220535L;
    private int items = 0;
    private ArrayList<Rate> rates = new ArrayList<Rate>();
    private boolean isRateNull = true;
    private int selectIndex = Rate.INVALID_ISPUBLICCHAREGE_ID;

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    public ArrayList<Rate> getRates() {
        return rates;
    }

    public void setRates(ArrayList<Rate> rates) {
        this.rates = rates;
    }

    public boolean isRateNull() {
        return isRateNull;
    }

    public void setRateNull(boolean isRateNull) {
        this.isRateNull = isRateNull;
    }

    public int getItems() {
        return items;
    }

    public void setItems(int items) {
        this.items = items;
    }

    //this is for support lua data
	public static Rates generateRates(byte[] data, boolean isDataCompile) {
		Rates rates = new Rates();
		LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
		rates.items = CommonUtils.objectToInt(luaTable.rawget("items"));
		if (rates.items <= 0) {
			return rates;
		}
		rates.isRateNull = false;
		LuaTable list = (LuaTable) luaTable.rawget("list");
		Vector<LuaTable> dataList = LuaTableUtil.getArray(list);
        ArrayList<Rate> rateBills = new ArrayList<Rate>();
        for (LuaTable billData : dataList) {
            Rate r = new Rate();
            try {
                r.setRateCodeId(Integer.parseInt(new DecimalFormat("0")
                        .format((Double) billData.rawget("rateCode"))));
                r.setRateName((String) billData.rawget("rateName"));
                r.setIsPublicChargeId(Integer.parseInt(new DecimalFormat("0")
                        .format((Double) billData.rawget("isPublicCharge"))));
                if (billData.rawget("isDefault") != null) {
                    r.setIsDefault((String) billData.rawget("isDefault"));
                }
            } catch (Exception e) {
                return null;
            }
            if (null != r) {
                rateBills.add(r);
            }
        }
        rates.rates = rateBills;
		return rates;
	}

    public static Rates generateRates(RateListBean rateList) {
        Rates rates = new Rates();
//		LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);

        rates.items = rateList.getItems();
        if (rates.items <= 0) {
            return rates;
        }
        rates.isRateNull = false;
        List<RateListBean.ListBean> dataList = rateList.getList();
//		Vector<LuaTable> dataList = LuaTableUtil.getArray(list);
        rates.rates = Rate.generateListRates(dataList);
        return rates;
    }

    //生成测试数据
    public static Rates generateTestRates() {
        Rates rates = new Rates();
        rates.items = 4;
        if (rates.items <= 0) {
            return rates;
        }
        rates.isRateNull = false;
        List<RateListBean.ListBean> dataList = new ArrayList<>();
        for (int i = 0; i <= rates.items - 1; i++) {
            RateListBean.ListBean lb = new RateListBean.ListBean();
            lb.setRateCode(String.valueOf(i));
            lb.setRateName("费率费率费率费率费率费率费率" + i);
            lb.setRateAmount("1000");
            lb.setIsDefault(i == 0 ? "1" : "0");
            lb.setIsPublicCharge("1");
            dataList.add(lb);
        }
        HPLog.i(HPLog.TAG_RESULT, "-----generateTestRates---dataList.size:" + dataList.size());
//		Vector<LuaTable> dataList = LuaTableUtil.getArray(list);
        rates.rates = Rate.generateListRates(dataList);
        return rates;
    }

}
